// Display a popup overlay with a text area for the coach to write comments in.
function addComment(eventID, swimmerID) {
	// Get the ajax object.
	var xmlhttp = getAjaxObject();
	
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById("blackOverlay").style.display='block';
			// Display the comment box as an overlay that covers the page.
			document.getElementById("commentBox").style.display='block';
			// Reset the box styling because saveComments() makes it smaller.
			document.getElementById("commentBox").style.left='27%';
			document.getElementById("commentBox").style.width='46%';
			document.getElementById("commentBox").style.height='50%';
			// Display the coaches name and the comment in the comment box that pops up.
			document.getElementById("commentText").innerHTML = xmlhttp.responseText;
		}
	}
	
	var data = "eventID=" + eventID + "&swimmerID=" + swimmerID;
	// Send the event id to the php script which gets the coaches name
	// and the comment text to be displayed.
	xmlhttp.open("POST", "./ajax_enter_comments/processEnterComments.php", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(data);
}

// Close the comment box by hiding it.
function closeBox() {
	document.getElementById("commentBox").style.display='none';
	document.getElementById("blackOverlay").style.display='none';
}

// Take the comment data and save it to the database.
function saveComments(eventID, swimmerID, managerID) {//window.location = "http://www.w3schools.com";
	var xmlhttp = getAjaxObject();
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			// Cover the whole page with a black overlay.
			document.getElementById("blackOverlay").style.display='block';
			// Display the comment box as an overlay that covers the page.
			document.getElementById("commentBox").style.display='block';
			// Make the comment box smaller.
			document.getElementById("commentBox").style.left='37.5%';
			document.getElementById("commentBox").style.width='25%';
			document.getElementById("commentBox").style.height='100px';
			// Display the swimmers name and the comment in the comment box that pops up.
			document.getElementById("commentText").innerHTML = xmlhttp.responseText;
		}
	}
	var theComment = document.getElementById("theComment").value;
	var data = "eventID=" + eventID + "&swimmerID=" + swimmerID + 
		"&managerID=" + managerID + "&theComment=" + theComment;
	// Send the comment data to the php script which saves it to the database.
	xmlhttp.open("POST", "./ajax_enter_comments/saveComments.php", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(data);
}

// Create and return an ajax object.
function getAjaxObject() {
	var xmlhttp = null;
	try {
		xmlhttp = new XMLHttpRequest();
	}
	catch(err) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(err) {
			alert("Error, your browser does not support AJAX");
		}
	}
	return xmlhttp;
}